require 'uri'
require 'net/http'
require 'json'
require 'pp'

def sever_host
  'http://10.13.55.199:5051'
end

def git_commit
  '970a15737273ef118922cea2a375c4b4b6d2aafd'
end

def project_id
  '14975'
end

def mr_id
  12
end

def hello
  url = URI("http://10.13.55.199:5051/hello")
  http = Net::HTTP.new(url.host, url.port)
  request = Net::HTTP::Get.new(url)
  response = http.request(request)
  puts response.read_body
end

# 
# POST /nicolas HTTP/1.1
# Host: 127.0.0.1
# Content-Type: application/json
# {
#   "action" : "merge_request_begin",
#   "value" : {
#     "git_commit" : "gitcommit001",
#     "project_id" : "849",
#     "mr_id" : "8687"
#   }
# }
# ------WebKitFormBoundary7MA4YWxkTrZu0gW--
# 
def mr_begin
  url = URI("#{sever_host}/nicolas")
  http = Net::HTTP.new(url.host, url.port)
  request = Net::HTTP::Post.new(url)

  request["Content-Type"] = 'application/json'
  request["cache-control"] = 'no-cache'
  request["Postman-Token"] = '1fe44fbc-733b-4a06-a345-28842ee92f72'

  # ruby object
  body = {
    action: 'merge_request_begin',
    value: {
      git_commit: git_commit,
      project_id: project_id,
      mr_id: mr_id,
      build_number: 18889,
      build_url: 'http://ci.in.zhihu.com/job/iOS-Platform-Zhihu-MR/163/'
    }
  }

  # post body json 字符串
  request.body = JSON.generate(body)

  response = http.request(request)
  puts response.read_body
end

# 
# POST /nicolas HTTP/1.1
# Host: 127.0.0.1
# Content-Type: application/json
# {
#   "action" : "jira_check_failed",
#   "value" : {
#     "git_commit" : "gitcommit001",
#     "project_id" : "849",
#     "mr_id" : "8687",
#     "message" : "jira check failed"
#   }
# }
# ------WebKitFormBoundary7MA4YWxkTrZu0gW--
# 
def jira_failed
  url = URI("#{sever_host}/nicolas")
  http = Net::HTTP.new(url.host, url.port)
  request = Net::HTTP::Post.new(url)

  request["Content-Type"] = 'application/json'
  request["cache-control"] = 'no-cache'
  request["Postman-Token"] = '1fe44fbc-733b-4a06-a345-28842ee92f72'

  # ruby object
  body = {
    action: 'jira_check_failed',
    value: {
      git_commit: git_commit,
      project_id: project_id,
      mr_id: mr_id,
      message: 'jira check failed'
    }
  }

  # post body json 字符串
  request.body = JSON.generate(body)

  response = http.request(request)
  puts response.read_body
end

# 
# POST /nicolas HTTP/1.1
# Host: 127.0.0.1
# Content-Type: application/json
# {
#   "action" : "podfile_analyze_begin",
#   "value" : {
#     "git_commit" : "gitcommit001",
#     "project_id" : "849",
#     "mr_id" : "8687",
#   }
# }
# ------WebKitFormBoundary7MA4YWxkTrZu0gW--
# 
def begin_denpendency_analyze
  url = URI("#{sever_host}/nicolas")
  http = Net::HTTP.new(url.host, url.port)
  request = Net::HTTP::Post.new(url)

  request["Content-Type"] = 'application/json'
  request["cache-control"] = 'no-cache'
  request["Postman-Token"] = '1fe44fbc-733b-4a06-a345-28842ee92f72'

  # ruby object
  body = {
    action: 'podfile_analyze_begin',
    value: {
      git_commit: git_commit,
      project_id: project_id,
      mr_id: mr_id,
    }
  }

  # post body json 字符串
  request.body = JSON.generate(body)

  response = http.request(request)
  puts response.read_body
end

# 
# POST /nicolas HTTP/1.1
# Host: 127.0.0.1
# Content-Type: application/json
# {
#   "action" : "podfile_analyze_begin",
#   "value" : {
#     "git_commit" : "gitcommit001",
#     "project_id" : "849",
#     "mr_id" : "8687",
#     "result" : {
#     	"title" : "Podfile 文件依赖分析",
#     	"value" : "我是分析后的结果!!!!"
#     }
#   }
# }
# ------WebKitFormBoundary7MA4YWxkTrZu0gW--
# 
def end_denpendency_analyze
  url = URI("#{sever_host}/nicolas")
  http = Net::HTTP.new(url.host, url.port)
  request = Net::HTTP::Post.new(url)

  request["Content-Type"] = 'application/json'
  request["cache-control"] = 'no-cache'
  request["Postman-Token"] = '1fe44fbc-733b-4a06-a345-28842ee92f72'

  # ruby object
  body = {
    action: 'podfile_analyze_end',
    value: {
      git_commit: git_commit,
      project_id: project_id,
      mr_id: mr_id,
      result: {
        title: 'Podfile 文件依赖分析',
        value: '!!!!!≈我是分析后的结果!!!!'
      }
    }
  }
  # pp body
  # puts body.class

  # post body json 字符串
  request.body = JSON.generate(body)

  response = http.request(request)
  puts response.read_body
end

# 
# POST /nicolas HTTP/1.1
# Host: 127.0.0.1
# Content-Type: application/json
# {
#   "action" : "begin_pod_install",
#   "value" : {
#     "git_commit" : "gitcommit001",
#     "project_id" : "849",
#     "mr_id" : "8687",
#   }
# }
# ------WebKitFormBoundary7MA4YWxkTrZu0gW--
# 
def begin_pod_install
  url = URI("#{sever_host}/nicolas")
  http = Net::HTTP.new(url.host, url.port)
  request = Net::HTTP::Post.new(url)

  request["Content-Type"] = 'application/json'
  request["cache-control"] = 'no-cache'
  request["Postman-Token"] = '1fe44fbc-733b-4a06-a345-28842ee92f72'

  # ruby object
  body = {
    action: 'pod_install_begin',
    value: {
      git_commit: git_commit,
      project_id: project_id,
      mr_id: mr_id
    }
  }

  # post body json 字符串
  request.body = JSON.generate(body)

  response = http.request(request)
  puts response.read_body
end

# 
# POST /nicolas HTTP/1.1
# Host: 127.0.0.1
# Content-Type: application/json
# {
#   "action" : "pod_install_ing",
#   "value" : {
#     "git_commit" : "gitcommit001",
#     "project_id" : "849",
#     "mr_id" : "8687",
#     "pod_name" : "YYModel"
#   }
# }
# ------WebKitFormBoundary7MA4YWxkTrZu0gW--
# 
def pod_installing(pod)
  url = URI("#{sever_host}/nicolas")
  http = Net::HTTP.new(url.host, url.port)
  request = Net::HTTP::Post.new(url)

  request["Content-Type"] = 'application/json'
  request["cache-control"] = 'no-cache'
  request["Postman-Token"] = '1fe44fbc-733b-4a06-a345-28842ee92f72'

  # ruby object
  body = {
    action: 'pod_install_ing',
    value: {
      git_commit: git_commit,
      project_id: project_id,
      mr_id: mr_id,
      pod: pod
    }
  }

  # post body json 字符串
  request.body = JSON.generate(body)

  response = http.request(request)
  puts response.read_body
end

def pod_install_end(state)
  url = URI("#{sever_host}/nicolas")
  http = Net::HTTP.new(url.host, url.port)
  request = Net::HTTP::Post.new(url)

  request["Content-Type"] = 'application/json'
  request["cache-control"] = 'no-cache'
  request["Postman-Token"] = '1fe44fbc-733b-4a06-a345-28842ee92f72'

  # ruby object
  body = {
    action: 'pod_install_end',
    value: {
      project_id: project_id,
      git_commit: git_commit,
      mr_id: mr_id,
      state: state,
      error_info: 'error install ffemped'
    }
  }

  # post body json 字符串
  request.body = JSON.generate(body)

  response = http.request(request)
  puts response.read_body
end

# 
# POST /nicolas HTTP/1.1
# Host: 127.0.0.1
# Content-Type: application/json
# {
#   "action" : "podfile_analyze_begin",
#   "value" : {
#     "git_commit" : "gitcommit001",
#     "project_id" : "849",
#     "mr_id" : "8687",
#   }
# }
# ------WebKitFormBoundary7MA4YWxkTrZu0gW--
# 
def begin_build
  url = URI("#{sever_host}/nicolas")
  http = Net::HTTP.new(url.host, url.port)
  request = Net::HTTP::Post.new(url)

  request["Content-Type"] = 'application/json'
  request["cache-control"] = 'no-cache'
  request["Postman-Token"] = '1fe44fbc-733b-4a06-a345-28842ee92f72'

  # ruby object
  body = {
    action: 'build_begin',
    value: {
      git_commit: git_commit,
      project_id: project_id,
      mr_id: mr_id
    }
  }

  # post body json 字符串
  request.body = JSON.generate(body)

  response = http.request(request)
  puts response.read_body
end

# 
# POST /nicolas HTTP/1.1
# Host: 127.0.0.1
# Content-Type: application/json
# {
#   "action" : "build_ing",
#   "value" : {
#     "git_commit" : "gitcommit001",
#     "project_id" : "849",
#     "mr_id" : "8687",
#     "target" : "YYModel"
#   }
# }
# ------WebKitFormBoundary7MA4YWxkTrZu0gW--
# 
def building(build_target)
  url = URI("#{sever_host}/nicolas")
  http = Net::HTTP.new(url.host, url.port)
  request = Net::HTTP::Post.new(url)

  request["Content-Type"] = 'application/json'
  request["cache-control"] = 'no-cache'
  request["Postman-Token"] = '1fe44fbc-733b-4a06-a345-28842ee92f72'

  # ruby object
  body = {
    action: 'build_ing',
    value: {
      git_commit: git_commit,
      project_id: project_id,
      mr_id: mr_id,
      target: build_target
    }
  }

  # post body json 字符串
  request.body = JSON.generate(body)

  response = http.request(request)
  puts response.read_body
end

# 
# POST /nicolas HTTP/1.1
# Host: 127.0.0.1
# Content-Type: application/json
# {
#   "action" : "build_success_end",
#   "value" : {
#     "git_commit" : "gitcommit001",
#     "project_id" : "849",
#     "mr_id" : "8687",
#     "build_number" : "19999",
#     "build_url" : "http://ci.in.zhihu.com/job/zhihu-iOS-mr/24527",
#     "ipa_url" : "https://test-pkg.zhimg.com/zhihu/osee2unifiedRelease-4.29.0-24527-20181120_140531.ipa?auth_key=1550642734-0-0-960254eea3a96c1154ba888ff1f0a34f",
#     "ipa_size" : "90.52 M",
#     "qrcode_url" : "https://chart.googleapis.com/chart?chl=http://tinyurl.com/ybtgs4cn&chs=100x100&cht=qr"
#   }
# }
# ------WebKitFormBoundary7MA4YWxkTrZu0gW--
# 
def build_success_end
  url = URI("#{sever_host}/nicolas")
  http = Net::HTTP.new(url.host, url.port)
  request = Net::HTTP::Post.new(url)

  request["Content-Type"] = 'application/json'
  request["cache-control"] = 'no-cache'
  request["Postman-Token"] = '1fe44fbc-733b-4a06-a345-28842ee92f72'

  # ruby object
  body = {
    action: 'build_success_end',
    value: {
      git_commit: git_commit,
      project_id: project_id,
      mr_id: mr_id,
      build_number: '19999',
      build_url: 'http://ci.in.zhihu.com/job/zhihu-iOS-mr/24527',
      ipa_url: 'https://test-pkg.zhimg.com/zhihu/osee2unifiedRelease-4.29.0-24527-20181120_140531.ipa?auth_key=1550642734-0-0-960254eea3a96c1154ba888ff1f0a34f',
      ipa_size: '90.52 M',
      qrcode_url: 'https://chart.googleapis.com/chart?chl=http://tinyurl.com/ybtgs4cn&chs=100x100&cht=qr',
      # jenkins_log_url: 'http://ci.in.zhihu.com/job/zhihu-iOS-mr/24527/consoleText'
    }
  }

  # post body json 字符串
  request.body = JSON.generate(body)

  response = http.request(request)
  puts response.read_body
end

def build_fail_end(error_info)
  url = URI("#{sever_host}/nicolas")
  http = Net::HTTP.new(url.host, url.port)
  request = Net::HTTP::Post.new(url)

  request["Content-Type"] = 'application/json'
  request["cache-control"] = 'no-cache'
  request["Postman-Token"] = '1fe44fbc-733b-4a06-a345-28842ee92f72'

  # ruby object
  body = {
    action: 'build_fail_end',
    value: {
      project_id: project_id,
      git_commit: git_commit,
      mr_id: mr_id,
      error_info: error_info
    }
  }

  # post body json 字符串
  request.body = JSON.generate(body)

  response = http.request(request)
  puts response.read_body
end

# 
# POST /nicolas HTTP/1.1
# Host: 127.0.0.1
# Content-Type: application/json
# {
#   "action" : "ci_warehouse",
#   "value" : {
#     "git_commit" : "gitcommit001",
#     "project_id" : "849",
#     "mr_id" : "8687",
#     "ci_warehouse_url" : "http://ci-warehouse.dev.zhihu.com/startup/detail?mr_gitlab_id=9049&commit=171a1a72&package_id=1730"
#   }
# }
# ------WebKitFormBoundary7MA4YWxkTrZu0gW--
# 
def ci_warehouse
  url = URI("#{sever_host}/nicolas")
  http = Net::HTTP.new(url.host, url.port)
  request = Net::HTTP::Post.new(url)

  request["Content-Type"] = 'application/json'
  request["cache-control"] = 'no-cache'
  request["Postman-Token"] = '1fe44fbc-733b-4a06-a345-28842ee92f72'

  # ruby object
  body = {
    action: 'ci_warehouse',
    value: {
      git_commit: git_commit,
      project_id: project_id,
      mr_id: mr_id,
      ci_warehouse_url: 'http://ci-warehouse.dev.zhihu.com/startup/detail?mr_gitlab_id=9049&commit=171a1a72&package_id=1730'
    }
  }

  # post body json 字符串
  request.body = JSON.generate(body)

  response = http.request(request)
  puts response.read_body
end

# 
# GET /nicolas/849/8687/gitcommit001 HTTP/1.1
# Host: 127.0.0.1
# 
def get_build_data
  url = URI("#{sever_host}/nicolas/#{project_id}/#{mr_id}/#{git_commit}")
  http = Net::HTTP.new(url.host, url.port)
  request = Net::HTTP::Get.new(url)
  request["content-type"] = 'application/json'
  response = http.request(request)
  puts response.read_body
end

# def get_denpendency_analyze
#   url = URI("http://10.13.55.199:5051/get_denpendency_analyze/849/gitcommit001/8687")
#   http = Net::HTTP.new(url.host, url.port)
#   request = Net::HTTP::Get.new(url)
#   response = http.request(request)
#   puts response.read_body
# end

def task
  # mr_begin
  # sleep 1
  # jira_failed
  # sleep 1
  # begin_denpendency_analyze
  # sleep 1
  # end_denpendency_analyze
  # sleep 1
  # begin_pod_install
  # sleep 1
  # pod_installing 'AFNetworking'
  # sleep 1
  # pod_installing 'YYModel'
  # sleep 1
  # pod_installing 'XZHKit'
  # sleep 1
  # pod_install_end('error')
  # pod_install_end('ok')
  # sleep 1
  # begin_build
  # sleep 1
  # building 'YYModel'
  # sleep 1
  # building 'Linking'
  # sleep 1
  build_fail_end('undef xxxxxx')
  # build_success_end
  # sleep 1
  # ci_warehouse
end

if __FILE__ == $0
  # hello
  task
  # get_build_data
end
